package com.stock.stockana.requests;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.stock.stockana.beans.ResponseBean;
import com.stock.stockana.beans.StockInfo;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.GetRecStocksEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * ---------------------------------------------------
 * Description: 获取用户所有的stock的请求类
 * Author: ljgsonx
 * Belong to: com.wds.stock.stockana.requests
 * Time: 2016/1/15 17:42
 * ---------------------------------------------------
 */
public class GetRecStocks extends BaseStringRequest<GetRecStocks> {

    private final static String TAG = "GetRecStocks--->";
    public static List<StockInfo> stocks = new ArrayList<>();

    public final static String GET_MODE_DEC = "0";
    public final static String GET_MODE_RIS = "1";
    public final static String GET_MODE_ALL = "2";

    private String recentDays = "15";
    private String percentLimit = "6";
    private String decOrrise = GET_MODE_DEC;

    private GetRecStocks(){
        super(Method.POST, NetWorkConstants.GET_RECOMINVOICES_URL, listener, errorListener);
        Log.d(TAG, NetWorkConstants.GET_RECOMINVOICES_URL);
    }

    static Response.Listener<String> listener = new Response.Listener<String>(){

        @Override
        public void onResponse(String response) {
            if("[".charAt(0) == response.charAt(0)) {
                List<StockInfo> tempStocks = JSON.parseArray(response, StockInfo.class);
                stocks.clear();
                stocks.addAll(tempStocks);
                EventBus.getDefault().post(new GetRecStocksEvent(NetWorkConstants.RESPONSE_CODE_OK, null));
            }else if("{".charAt(0) == response.charAt(0)){
                ResponseBean rb = JSON.parseObject(response, ResponseBean.class);
                EventBus.getDefault().post(new GetRecStocksEvent(NetWorkConstants.RESPONSE_CODE_ERROR,NetWorkConstants.RESPONSE_ERROR2 +rb.getErrorMsg()));
            }else{
                EventBus.getDefault().post(new GetRecStocksEvent(NetWorkConstants.RESPONSE_CODE_ERROR,NetWorkConstants.RESPONSE_ERROR1 + response));
            }
        }
    };

    static Response.ErrorListener errorListener = new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            EventBus.getDefault().post(new GetRecStocksEvent(NetWorkConstants.RESPONSE_CODE_ERROR, NetWorkConstants.RESPONSE_ERROR3 + error.toString()));
        }
    };

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> map = new HashMap<>();
        map.put("recentDays", recentDays);
        map.put("percentLimit", percentLimit);
        map.put("decOrrise", decOrrise);
        return map;
    }

    @Override
    public Request<?> setTag(Object tag) {
        return super.setTag(TAG);
    }

    public static String getTagStr(){
        return TAG;
    }

    public static GetRecStocks build() {
        return new GetRecStocks();
    }

    public GetRecStocks setRecentDays(String recentDays) {
        this.recentDays = recentDays;
        return this;
    }

    public GetRecStocks setPercentLimit(String percentLimit) {
        this.percentLimit = percentLimit;
        return this;
    }

    public GetRecStocks setChooseMode(String mode) {
        this.decOrrise = mode;
        return this;
    }

}
